package com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView;


import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemModelDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;

//最终显示的menu
public class JsMenuItemViewCDefault extends JsMenuItemView {
    public ImageView miv;
    public TextView mtv_title;
    public TextView mtv_description;
    private RelativeLayout mlc_root;

    public JsMenuItemViewCDefault(Context context) {
        super(context);

    }

    @Override
    public void customSetVm(Context context, JsMenuItemVm vm) {
        {
            //mcl_root
            if(mlc_root == null) {
                mlc_root = new RelativeLayout(context);
                mlc_root.setId(generateViewId());
                addView(mlc_root);
                mlc_root.setGravity(Gravity.CENTER);
            }
        }
        //根据vm进行显示
        {
            if(miv == null) {

                miv = new ImageView(getContext());
                miv.setId(generateViewId());
                mlc_root.addView(miv);
                miv.setBackgroundColor(Color.parseColor("#ffff0000"));
            }

        }
        {
            if(mtv_title == null) {
                mtv_title = new TextView(getContext());
                mtv_title.setId(generateViewId());
                mlc_root.addView(mtv_title);
            }

        }
        customUpdateUi(mvm);
    }

    public void customUpdateUi(JsMenuItemVm vm) {
        //如何判断是否是的?
        if(vm.getClass() != JsMenuItemVmDefault.class) {
            Log.w("JsMenuFragment","Vm and view is not match");
            return;
        }
        JsMenuItemVmDefault defaultVm = (JsMenuItemVmDefault) vm;

        JsMenuItemStyleDefault style = (JsMenuItemStyleDefault)defaultVm.currentStyle();
        JsMenuItemModelDefault model = (JsMenuItemModelDefault)defaultVm.currentModel();

        if(style.mI_backgroundColor != null) {
            setBackgroundColor(style.mI_backgroundColor);
        }

        if(model.mI_imageId == null) {
            miv.setVisibility(INVISIBLE);


            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            mtv_title.setLayoutParams(lp);

            mtv_title.setText(model.mStr_title);
            mtv_title.setGravity(Gravity.CENTER);

        }
        mtv_title.setTextColor(style.mI_titleTextColor);
        if(style.mI_backgroundColor != null) {
            setBackgroundColor(style.mI_backgroundColor);
        }
        mtv_title.setTextSize(style.mi_titleFontSize);



        //Todo: 这里不能修改ItemView的布局,会导致异常. 那要点击显示变大的效果,怎么处理?
//        // 设置Cell的高度
//        int width = ViewGroup.LayoutParams.WRAP_CONTENT;
//        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
//        if(defaultVm.mI_height != null) {
//            height = defaultVm.mI_height;
//        }
//        if(defaultVm.mI_width != null) {
//            width = defaultVm.mI_width;
//        }
//        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(width,height);
//        setLayoutParams(lp);
    }

}